package com.t2c.tibetantochinese.me;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseFragment;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.IntentUtil;
import com.t2c.tibetantochinese.utils.SPUtil;

/**
 * Created by zoulu on 16/6/18.
 * 我的
 */
public class MeFragment extends BaseFragment implements View.OnClickListener{

    private TextView tv_user;
    private TextView tv_notify;
    private TextView tv_share;
    private TextView tv_note;
    private TextView tv_feedback;
    private TextView tv_clean;
    private TextView tv_activity;
    private TextView tv_login;
    private TextView tv_logout;
    private TextView tv_code;
    private RelativeLayout re_user;
    private RelativeLayout re_logout;
    private RelativeLayout re_feedback;
    private RelativeLayout re_code;
    @Override
    protected int getContentViewResId() {
        return R.layout.me_layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    private void init(View view) {
        re_user = (RelativeLayout) view.findViewById(R.id.re_user);
        re_code = (RelativeLayout) view.findViewById(R.id.re_code);
        re_logout = (RelativeLayout) view.findViewById(R.id.re_logout);
        re_user.setOnClickListener(this);
        re_logout.setOnClickListener(this);
        tv_user = (TextView) view.findViewById(R.id.tv_user);
        tv_notify = (TextView) view.findViewById(R.id.tv_notify);
        tv_share = (TextView) view.findViewById(R.id.tv_share);
        tv_note = (TextView) view.findViewById(R.id.tv_note);
        tv_feedback = (TextView) view.findViewById(R.id.tv_feedback);
        tv_clean = (TextView) view.findViewById(R.id.tv_clean);
        tv_activity = (TextView) view.findViewById(R.id.tv_activity);
        tv_login = (TextView) view.findViewById(R.id.tv_login);
        re_feedback = (RelativeLayout) view.findViewById(R.id.re_feedback);
        tv_logout = (TextView) view.findViewById(R.id.tv_logout);
        tv_code = (TextView) view.findViewById(R.id.tv_code);
        if(!CommonUtil.isChinese(getActivity())){
            tv_user.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_feedback.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_activity.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_notify.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_share.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_note.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_clean.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_login.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_logout.setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_code.setTypeface(CommonUtil.getTibetanType(getActivity()));

            tv_user.setText(getResources().getString(R.string.taccount));
            tv_feedback.setText(getResources().getString(R.string.tfeedback));
            tv_activity.setText(getResources().getString(R.string.tactivity));
            tv_notify.setText(getResources().getString(R.string.tmessage));
            tv_share.setText(getResources().getString(R.string.tfriendcircle));
            tv_note.setText(getResources().getString(R.string.tchecknote));
            tv_clean.setText(getResources().getString(R.string.tclean));
            tv_login.setText(getResources().getString(R.string.tloginaccount));
            tv_logout.setText(getResources().getString(R.string.tlogout));
            tv_code.setText(getResources().getString(R.string.tcode));
        }
        tv_login.setOnClickListener(this);
        tv_logout.setOnClickListener(this);
        tv_user.setOnClickListener(this);
        re_feedback.setOnClickListener(this);
        re_code.setOnClickListener(this);

        if(!TextUtils.isEmpty(SPUtil.get(getContext(),"name","").toString())){
            tv_login.setText(SPUtil.get(getContext(),"name","").toString());
            re_logout.setVisibility(View.VISIBLE);
        }else {
            re_logout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_login:
                if(TextUtils.isEmpty(SPUtil.get(getContext(),"name","").toString())){
                    IntentUtil.gotoLogin(getActivity());
                }
                break;
            case R.id.re_logout:
                SPUtil.remove(getContext(),"name");
                Toast.makeText(getContext(),"退出登陆成功",Toast.LENGTH_SHORT).show();
                re_logout.setVisibility(View.GONE);
                if(!CommonUtil.isChinese(getActivity())){
                    tv_login.setTypeface(CommonUtil.getTibetanType(getActivity()));
                    tv_login.setText(getResources().getString(R.string.tloginaccount));
                }else{
                    tv_login.setText(getResources().getString(R.string.loginaccount));
                }
                break;
            case R.id.re_user:
                if(TextUtils.isEmpty(SPUtil.get(getContext(),"name","").toString())) {
                    IntentUtil.gotoLogin(getActivity());
                }
                break;
            case R.id.re_feedback:
                if(TextUtils.isEmpty(SPUtil.get(getContext(),"name","").toString())){
                    IntentUtil.gotoLogin(getActivity());
                }else{
                    IntentUtil.gotoFeedBack(getActivity());
                }
                break;
            case R.id.re_code:
                IntentUtil.gotoCode(getActivity());
                break;
            default:

                break;
        }
    }
}
